package service

import (
	"fmt"
	"time"

	"github.com/panjf2000/gnet/v2"
)

type Conn struct {
	gnet.Conn
	IsAuthValid    bool
	ConnectTime    time.Time
	LatestBeatTime time.Time
}

func newConn(c gnet.Conn) *Conn {
	return &Conn{
		Conn:        c,
		ConnectTime: time.Now(),
	}
}

func (s *ServiceImp) addConn(c gnet.Conn) {
	conn := newConn(c)
	s.connTree.Set(fmt.Sprintf("%v", c), conn)
}

func (s *ServiceImp) removeConn(c gnet.Conn) {
	key := fmt.Sprintf("%p", c)
	s.connTree.Delete(key)
}

func (s *ServiceImp) getConn(c gnet.Conn) *Conn {
	conn, has := s.connTree.Get(fmt.Sprintf("%p", c))
	if has {
		return conn
	} else {
		conn = newConn(c)
		s.connTree.Set(fmt.Sprintf("%p", c), conn)
		return conn
	}
}
